<?php
namespace ChannelThrift\Controller;
include 'WeaverAuctionTestCase.class.php';

use \Think\PhpUnit;
use \Thrift\Protocol\TBinaryProtocol;
use \Thrift\Protocol\TMultiplexedProtocol;
use \Thrift\Transport\THttpClient;
use \Thrift\Transport\TBufferedTransport;
use \Thrift\Exception\TException;

global $isClean;
$isClean = true;

/**
 * 保证金服务单元测试;
    使用示例:
    phpunit ../Tests/BailControllerTest.class.php
    phpunit ../Tests/BailControllerTest.class.php --group GetBail        
    phpunit ../Tests/BailControllerTest.class.php --group IncBail        
    phpunit ../Tests/BailControllerTest.class.php --group Freeze  
    phpunit ../Tests/BailControllerTest.class.php --group UnFreeze 
    phpunit ../Tests/BailControllerTest.class.php --group DecAndUnFreeze    
    phpunit ../Tests/BailControllerTest.class.php --group DecBail

 * @requires extension mysqli 
 * @requires extension redis
 * @requires extension PHP 5.4.45 
 */
class BailControllerTest extends WeaverAuctionTestCase
{

    private $client = null;
    private $redis = null;
    private $dealerID = 468;
    private $orderID = 2513;
    private $redisDatabase = 4;
    
    /**
        @param serviceName 服务模块名
        @param servicePath 服务路径
        @param serviceDomain 服务域名
        @param servicePort 服务端口
        @return thriftclient 
    */
    private function getThriftClient($serviceName,$servicePath,$serviceDomain,$servicePort=80)
    {      
        if (!$serviceName){
            //抛出异常            
            return false;
        }
        $handle = new THttpClient($serviceDomain, $servicePort, $servicePath);
        $transport = new TBufferedTransport($handle);
        $protocol = new TBinaryProtocol($transport);
        $auctionProtocol = new TMultiplexedProtocol($protocol, $serviceName);
        $serviceName='\\Services\\'.$serviceName.'\\'.$serviceName.'Client';
        $client = new $serviceName($auctionProtocol);
        return $client;        
    }

    private function getRedis(){        
        $config = require ROOT_PATH.'Application/ChannelThrift/Conf/redis.php';        
        $redis = new \Redis();
        $redis->connect($config['REDIS_HOST'],$config['REDIS_PORT']);
        $redis->select($this->redisDatabase);
        return $redis;
    }

    /**
        @before 
    */
    public function testThriftClient(){
        $client = $this->getThriftClient('BailService','/Bail/run','songaimin.auction.weaver.s.isou365.cn'); 
        $this->client = $client;
        $this->redis = $this->getRedis();
    }

    /**
        检查入队列的格式
    */
    private function chkQueueContent(&$QueueContent){
        $this->assertArrayHasKey('summary',$Bail);        
        $this->assertArrayHasKey('log',$Bail);
    }
    /**
        检查接口返回的格式        
    */
    private function chkThriftReturnBail(&$return){        
        $this->assertTrue(is_object($return));
        $this->assertInstanceOf('\Services\BailService\Bail',$return);
        $data  = $return->data;
        $this->assertArrayHasKey('dealer_id',$data);
        $this->assertArrayHasKey('bail_amount',$data);
        $this->assertArrayHasKey('freeze_amount',$data);
        $this->assertArrayHasKey('indemnity_amount',$data);
        $this->assertArrayHasKey('bail_amount',$data);
        $this->assertArrayHasKey('dealer_id',$data);
    }

    /**
        获取车商保证金概要验证
        @group GetBail
    */
    public function testGetDealerBail()
    {
        echo __METHOD__ ."\n";
        $client = $this->client;
        $dealerID = $this->dealerID;

        echo "从mysql获取\n";
        $Key = 'AuctionWeaver:Bail:DealerBailSummary:SpecVar:'.$dealerID;
        $redis = $this->redis;
        $redis->del($Key);
        $bailinfo = $client->getDealerBail($dealerID);
        $this->chkThriftReturnBail($bailinfo);
        $bailinfo = $bailinfo->data;

        $this->assertTrue($bailinfo['dealer_id'] == $dealerID);
        //print_r($bailinfo);
        

        echo "从redis获取\n";        
        $bailinfo = $client->getDealerBail($dealerID);        
        $this->chkThriftReturnBail($bailinfo);
        $bailinfo = $bailinfo->data;
        $this->assertTrue($bailinfo['dealer_id'] == $dealerID);        

        return $bailinfo;
    }

    
    
    /**
        充值验证
        @group IncBail
    */
    public function testIncBail()
    {
        echo __METHOD__ ."\n";
        
        $client = $this->client;   
        $dealerID = $this->dealerID;        
        $addAmount = 2000;
        $detail = '保证金充值';
        $rechargeNo = '100';
        $rechargeId = '1000';
        $rechargeMethod = '10000';
        $collectionId = '100000';
        $bail = $client->incBail($dealerID, $addAmount, $detail, $rechargeNo, $rechargeId, $rechargeMethod,$collectionId);            
                
    }

   
    /**
        测试冻结        
        @group Freeze
    */
    public function testFreezeBail()
    {
        echo __METHOD__ ."\n";
        
        $client = $this->client;   
        $dealerID = $this->dealerID;        
        $addAmount = 2000;
        $orderId = $this->orderID;
        
        $before_freeze_bail = $client->getDealerBail($dealerID);
        $before_freeze_bail = $before_freeze_bail->data;
        //执行冻结
        $bail = $client->freezeBail($dealerID, $addAmount, $orderId);
        
    }
   

    /**
        测试解冻保证金
        @group UnFreeze
    */
    public function testUnFreezeBail(){
        
        echo __METHOD__ ."\n";
        $client = $this->client;   
        $dealerID = $this->dealerID;                       
        $addAmount = 2000;
        $orderId = $this->orderID;
        $after_freeze_bail = $client->getDealerBail($dealerID);
        $after_freeze_bail = $after_freeze_bail->data;

        //执行解冻        
        $bail = $client->unfreezeBail($dealerID, $orderId, $addAmount);
    }
   

    /**
        从冻结保证金中，扣除并解冻
        @group DecAndUnFreeze
    */
    public function testUnFreezeDecBail()
    {
        echo __METHOD__ ."\n";
        
        $client = $this->client;   
        $redis = $this->redis;
        $dealerID = $this->dealerID;        
        $amount = 2000;
        $orderId = $this->orderID;
        $after_freeze_bail = $client->getDealerBail($dealerID);
        $after_freeze_bail = $after_freeze_bail->data;
        // print_r($after_freeze_bail);
        //扣除并解冻保证金
        $deductAmount = 1500;            
        $bail = $client->unfreezeDecBail($dealerID , $orderId , $deductAmount, 0, '');

    }

    /**
        测试保证金提取
        @group DecBail
    */
    public function testDecBail(){
        echo __METHOD__ ."\n";

        $client = $this->client;   
        $redis = $this->redis;
        $dealerID = $this->dealerID;        
        $amount = 500;
        $orderId = $this->orderID;
        //保证金提取
        $useDetail = '保证金提取';            
        $after_add_bail = $client->getDealerBail($dealerID)->data;
        $bail = $client->decBail($dealerID, $amount, 4, $useDetail,0,0,0,0);
    }

    /**
        清理测试的数据
        @afterClass
        @group clean 
    */
    public function testClean(){
        global $isClean;
        if ($isClean){
            echo __METHOD__ ."\n";
            $this->redis->del('AuctionWeaver:Bail:DealerBailSummary:SpecVar:'.$this->dealerID);
            $this->redis->del('AuctionWeaver:Bail:ChangeQueue');
            $this->redis->del('AuctionWeaver:Bail:FreezeDealerMap:SpecVar:'.$this->orderID);
            $this->redis->del('AuctionWeaver:Bail:DealerLock:SpecVar:'.$this->dealerID);    
        }
    }
   
}
